﻿Shader "Custom/Voxel Alpha" {
	Properties
	{
		_Color("Color", Color) = (1,1,1,1)
		_MainTex("Texture", 2D) = "white" {}
		_Power("偏移威力",Range(-0.01,0.01)) = -0.001
		_GridTex("背景框", 2D) = "white" {}
		_SelectTex("选择背景", 2D) = "white" {}
		_UV3Tex("选择背景", 2D) = "white" {}
	}
		SubShader
	{
		Tags{ "RenderType" = "Opaque" }
		Tags{ "Queue" = "Transparent+2" }
		LOD 100

	Pass
	{
		Blend SrcAlpha OneMinusSrcAlpha
		CGPROGRAM
		#pragma vertex vert 
		#pragma fragment frag
		// make fog work
		#pragma multi_compile_fog 

		//#include "UnityCG.cginc"
        #include "Lighting.cginc"

		struct appdata
		{
			float4 vertex : POSITION;
			float2 uv : TEXCOORD0;
			float2 uv2 : TEXCOORD1;
			float2 uv3 : TEXCOORD2;
			float4 color : COLOR0;
			float3 normal : NORMAL;
		};

		struct v2f
		{
			float2 uv : TEXCOORD0;
			float2 uv2 : TEXCOORD1;
			float2 uv3 : TEXCOORD2;
			//UNITY_FOG_COORDS(1)
			float4 vertex : SV_POSITION;
			float4 color : COLOR0;
		};

		sampler2D _MainTex;
		float4 _MainTex_ST;
		fixed4 _Color;
		float _Power;
		sampler2D _GridTex;
		float4 _GridTex_ST;
		sampler2D _SelectTex;
		
		
		float4 _UV3Tex_ST;
		sampler2D _UV3Tex;

		v2f vert(appdata v)
		{
			v2f o;
			o.vertex = UnityObjectToClipPos(v.vertex + float4(v.normal * _Power, 0.1));
			o.uv = TRANSFORM_TEX(v.uv, _MainTex);
			o.uv2 = TRANSFORM_TEX(v.uv2, _GridTex);
			o.uv3 = TRANSFORM_TEX(v.uv3, _UV3Tex);
			UNITY_TRANSFER_FOG(o,o.vertex);
			o.color = v.color;
			//o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
			return o;
		}

		fixed4 frag(v2f i) : SV_Target
		{
			fixed4 col;
			if (i.color.a <= 0.4)
			{
				i.color.a = 1;
				fixed4 colorShadow = tex2D(_SelectTex, i.uv2);//TRANSFORM_TEX(v.uv2, _GridTex)
				if (colorShadow.r == colorShadow.g)//白色
				{
					col = tex2D(_MainTex, i.uv) * i.color * _Color;//* tex2D(_UV3Tex, i.uv3)
				}
				else
				{
					col = colorShadow;
				}
			}
			else
			{
				col = tex2D(_MainTex, i.uv) * tex2D(_GridTex, i.uv2)  * _Color* i.color * tex2D(_UV3Tex, i.uv3);
				//fixed4 colorGrid = tex2D(_GridTex, i.uv2);
				//if (colorGrid.r > 0.5)//白色
				//{
				//	col = tex2D(_MainTex, i.uv) * i.color ;
				//}
				//else
				//{
				//	col =  _Color;
				//}
			}


			// sample the texture
			/*fixed4 col = tex2D(_MainTex, i.uv)  * fixed4(i.color, _Color.a)  * _Color;
			col.a = _Color.a;*/
			// apply fog
			//UNITY_APPLY_FOG(i.fogCoord, col);
			//clip(i.worldPos.y - 0);
			return col;
		}
			ENDCG
		}
	}
		Fallback "Diffuse"
}
